﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BaseSet.Entity;
using BaseSet.Logic;
using BaseSet.Framework;
using BaseSet.Data;
using BaseSet.Framework.Config;
using System.Web.Caching;


namespace WebSite.Admin.MasterPage
{
    public partial class AdminInner : System.Web.UI.MasterPage
    {

        private Users user;
        private string relUrl = string.Empty;
        private FunctionItem currItem;
        private PowerItem currPowerItem;

        protected void Page_Init(object sender, EventArgs e)
        {
            user = UserBase.GetCurrentAdminUser();
            if (user == null || user.ID.Length == 0)
            {
                Response.Redirect(UserBase.GetServerPath() + "/Admin/login.aspx");
            }

            string url = Request.Url.AbsoluteUri.Replace(UserBase.GetServerPath(), "").Trim();


            #region 这里返回当前用户是否对当前页有权限

            // 当前菜单 功能菜单的页权限
            FunctionItemHelper fiHelper = new FunctionItemHelper();
            //根据当前URL取得当前FunctionItem
            currItem = fiHelper.GetCurrFunItem(url);

            if (currItem != null)
            {

                PowerItemHelper piHelper = new PowerItemHelper();
                List<PowerItem> currPowerItems = piHelper.GetCurrPowerItem(user);

                bool havePower = false;
                if (currPowerItems != null && currPowerItems.Count > 0)
                {
                    foreach (PowerItem pi in currPowerItems)
                    {
                        if (pi.ItemID == currItem.ID)
                        {
                            havePower = true;
                            currPowerItem = pi;
                            break;
                        }
                    }
                }

                if (!havePower)
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(),
                    "NoPerm", "<script language='javascript'>alert('您没有权限访问该页！');history.go(-1);</script>");
                    return;
                }

            }
            else
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(),
                   "NoPerm", "<script language='javascript'>alert('您访问的页面不存在！');history.go(-1);</script>");
                return;
            }


            #endregion
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //加载导航
                // [业务中心]-[我的邮件]

                string nav = string.Empty;
                if (currItem != null)
                {
                    Left1.CurrMenu = currItem;
                    nav = currItem.ItemName;
                    FunctionsHelper fHelper = new FunctionsHelper();
                    List<Functions> fc = fHelper.GetCurrPowerFun(user);

                    foreach (Functions fun in fc)
                    {
                        if (fun.ID == currItem.FunctionID)
                        {
                            nav = fun.FunctionName + " >> " + nav;

                            if (fun.ParentID != "0")
                            {
                                nav = GetNavStr(fun, fc) + " >> " + nav;
                            }
                            else
                            {
                                Left1.TopMenu = fun;
                            }

                            break;
                        }
                    }

                    litNav.Text = nav;
                }

            }

        }

        private string GetNavStr(Functions fun, List<Functions> fc)
        {
            string nav = "";
            if (fun.ParentID != "0")
            {
                if (fc != null && fc.Count > 0)
                {
                    foreach (Functions tempfun in fc)
                    {
                        if (tempfun.ID == fun.ParentID)
                        {
                            nav = GetNavStr(tempfun, fc) + "" + tempfun.FunctionName + "";
                            break;
                        }
                    }

                }
            }
            else
            {
                Left1.TopMenu = fun;
            }

            return nav;

        }

    }
}